<?php

namespace App\Http\Requests;

use App\Rules\CaptchaCode;
use App\Rules\CaptchaKey;
use App\Rules\RefCodeValidate;
use App\Rules\SendSms;
use App\Rules\SendSmsKey;

class AuthRegisterRequest extends FormRequest
{


    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'name' => 'required|unique:users,name|regex:/^[0-9a-zA-Z]{3,40}$/',
            'password' => [
                'required',
                'regex:/^[0-9a-zA-Z]{6,20}$/',
                'confirmed'
            ],
            'password_confirmation'=> [
                'required','regex:/^[0-9a-zA-Z]{6,20}$/','same:password',
            ],
            'asset_password' => [
                'required',
                'regex:/^[0-9a-zA-Z]{6,20}$/',
                'confirmed'
            ],
            'asset_password_confirmation' => [
                'required',
                'regex:/^[0-9a-zA-Z]{6,20}$/',
                'same:asset_password'
            ],
            'ref_code'=>[new RefCodeValidate()],
            'captcha_key' => ['required','string'],
            'captcha_code' => ['required','string','captcha'],
            'type'=>'required|in:phone,email'
        ];
        if ($this->type == 'phone') {
            $rules['phone_key'] = 'required,string';
            $rules['phone_code'] = 'required|string|phone';
        } else {
            // todo 暫時沒做
            $rules['email_key'] = 'required|string';
            $rules['email_code'] = 'required|string|emailCode';
        }
        return $rules;
    }

    public function messages()
    {
        return [
            'name.required'=>['error_code'=>'F001001','message'=>'用户名不能为空！'],
            'name.unique'=>['error_code'=>'F001002','message'=>'用户名已存在！'],
            'name.regex'=>['error_code'=>'F001003','message'=>'用户名格式不正确！'],
            'phone.required'=>['error_code'=>'F002002','message'=>'手机号码不能为空！'],
            'phone.regex'=>['error_code'=>'F002003','message'=>'手机号码格式不正确！'],
            'phone.unique'=>['error_code'=>'F002004','message'=>'手机号码已存在！'],
            'password.required'=>['error_code'=>'F003001','message'=>'密码不能为空！'],
            'password.regex'=>['error_code'=>'F003002','message'=>'密码格式不正确！'],
            'password.confirmed'=>['error_code'=>'F003003','message'=>'两次密码不一致！'],
            'password_confirmation.required'=>['error_code'=>'F003004','message'=>'确认密码不能为空！'],
            'password_confirmation.regex'=>['error_code'=>'F003005','message'=>'确认密码格式不正确！'],
            'password_confirmation.same'=>['error_code'=>'F003006','message'=>'两次密码不一致！'],
            'asset_password.required'=>['error_code'=>'F003007','message'=>'资金密码不能为空！'],
            'asset_password.regex'=>['error_code'=>'F003008','message'=>'资金密码格式不正确！'],
            'asset_password.confirmed'=>['error_code'=>'F003009','message'=>'资金密码不一致！'],
            'asset_password_confirmation.required'=>['error_code'=>'F003010','message'=>'确认资金密码不能为空！'],
            'asset_password_confirmation.regex'=>['error_code'=>'F003011','message'=>'确认资金密码格式不正确！'],
            'asset_password_confirmation.same'=>['error_code'=>'F003012','message'=>'确认资金密码不一致！'],
            'captcha_key.required'=>['error_code'=>'F004001','message'=>'图片验证码 key 不能为空！'],
            'captcha_key.string'=>['error_code'=>'F004002','message'=>'图片验证码 key 必须是字符串！'],
            'captcha_code.required'=>['error_code'=>'F004003','message'=>'图片验证码不能为空！'],
            'captcha_code.string'=>['error_code'=>'F004004','message'=>'图片验证码必须是字符串！'],
            'phone_key.required'=>['error_code'=>'F002005','message'=>'手机验证码 key 不能为空！'],
            'phone_key.string'=>['error_code'=>'F002006','message'=>'手机验证码 key 必须是字符串！'],
            'phone_code.required'=>['error_code'=>'F002007','message'=>'手机验证码不能为空！'],
            'phone_code.string'=>['error_code'=>'F002008','message'=>'手机验证码必须是字符串！'],
            'phone_code.phone'=>['error_code'=>'F002009','message'=>'手机验证码错误！'],
//            'phone_key.phone_key'=>['error_code'=>'F002010','message'=>'手机验证码 key 过期！'],
            'captcha_code.captcha'=>['error_code'=>'F004005','message'=>'图片验证码错误！'],
            'type.required'=>['error_code'=>'F004006','message'=>'注冊類型不能為空！'],
            'type.in'=>['error_code'=>'F004007','message'=>'只能使用 phone | email 兩種方式註冊！'],

            'email_key.required'=>['error_code'=>'F004008','message'=>'邮箱驗證碼 key 不能为空！'],
            'email_key.string'=>['error_code'=>'F002009','message'=>'邮箱驗證碼 key 不能为空！'],
            'email_code.required'=>['error_code'=>'F002010','message'=>'邮箱驗證碼 不能为空！'],
            'email_code.string'=>['error_code'=>'F002011','message'=>'邮箱驗證碼 不能为空！'],
            'email_code.emailCode'=>['error_code'=>'F002012','message'=>'邮箱驗證碼 錯誤！'],

//            'captcha_key.captcha_key'=>['error_code'=>'F004006','message'=>'图片验证码 key 过期！'],


        ];
    }
}
